/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sp.controller;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import sp.componentButton.Closebutton;
import sp.componentButton.Savebutton;
import sp.model.Dsartb4;
import sp.model.TableEntryModel;
import sp.panelcomponent.EntryFormListingTest;
import sp.panelcomponent.EntryFormQuestTest;
import sp.util.CardLayoutController;
import sp.util.ErrorControl;
import sp.util.FormControl;
import sp.util.LogMessage;
import sp.view.ConfirmClose;
import sp.view.Panel;
import sp.view.TesBeta;

/**
 *
 * @author Tiara Ratna Dewi [12.7403]
 */
public class ButtonController {

    CardLayoutController controller;
    EntryFormQuestTest quest;
    EntryFormListingTest listing;
    Closebutton closebutton;
    Savebutton savebutton;
    TesBeta coba;
    Panel panel;
    JPanel Mainpanel;
    String nuart = "0";

    public ButtonController(TesBeta coba, EntryFormQuestTest quest, EntryFormListingTest listing, JPanel Mainpanel) {
        this.coba = coba;
        this.listing = listing;
        this.quest = quest;
        this.Mainpanel = Mainpanel;

        controller = new CardLayoutController();
        controller.setCardLayout((CardLayout) Mainpanel.getLayout());
        controller.setParentCard(Mainpanel);

        coba.getPanel1().getEntryQuest().getTableUpdate1().getTabelUpdate().addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                coba.getPanel1().getEntryQuest().getStartButton1().setEnabled(true);
            }
        });

        coba.getPanel1().getDataEntryQuest1().getStartButton1().addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int index = coba.getPanel1().getEntryQuest().getTableUpdate1().getTabelUpdate().getSelectedRow();
                if (index > -1) {
                    coba.getPanel1().getEntryQuest().getStartButton1().setEnabled(false);
                    loadDsart2Form(index);
                    coba.getEntryQuest().getCloseBtn().setEnabled(true);
                } else {
                    entryDariNol();
                }
            }

            private void loadDsart2Form(int row) {
                sp.model.Dsartb4 db4 = ((TableEntryModel) coba.getPanel1().getEntryQuest().getTableUpdate1().getTabelUpdate().getModel()).get(row);
                String key = db4.getNuart().substring(0, db4.getNuart().length() - 2);
                nuart = db4.getNuart();
                sp.model.Dsartb4 data4 = sp.dao.DsartB4Dao.getInstance().getByID(nuart);

                if ("0".equals(db4.getIsLocked())) {
                    changeLock(db4);
                    sp.model.Dsartb1 data1 = sp.dao.DsartB1Dao.getInstance().getByID(key);
                    sp.model.Dsartb3 data3 = sp.dao.DsartB3Dao.getInstance().getByID(key);
                    coba.getEntryQuest().getHal11().setFromListing(data1, data3, data4);
                    coba.getEntryQuest().getHal21().setFromListing(data4);
                    coba.getEntryQuest().getHal71().setFromListing();

                    String loadData = "";
                    try {
                        coba.getEntryQuest().getErrorTable1().getTabelError().setModel(sp.util.ErrorControl.getTableRow());
                    } catch (Exception e) {
                        loadData = e.getMessage();
                    }
                    try {
                        LogMessage.write(loadData);
                    } catch (IOException ex) {
                        Logger.getLogger(ButtonController.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    coba.getEntryQuest().getSaveBtn().setEnabled(true);
                    coba.getEntryQuest().getUpdateBtn().setEnabled(false);
                    controller.show("entryQuest");
                } else {
                    JOptionPane.showMessageDialog(coba.getEntryQuest(), "Sorry, data of " + db4.getNuart() + " is used by other user");
                }
            }

            private void entryDariNol() {
                coba.getEntryQuest().getSaveBtn().setEnabled(true);
                coba.getEntryQuest().getUpdateBtn().setEnabled(false);
                //FormControl.clearAll();
                FormControl.getController().first();
                controller.show("entryQuest");
            }

            private void changeLock(Dsartb4 o) {
                String locked = "1", unlocked = "0";
                if (o.getIsLocked().equals(unlocked)) {
                    o.setIsLocked(locked);
                } else {
                    o.setIsLocked(unlocked);
                }
                sp.dao.DsartB1Dao.getInstance().update(o);
            }
        });

        quest.getCloseBtn().addActionListener(new ActionListener() {

            int yakin = 0;

            @Override
            public void actionPerformed(ActionEvent ae) {

                ConfirmClose konfirm = new ConfirmClose(null, true);
                konfirm.getYa().addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        konfirm.setVisible(false);
                        ConfirmClose konfirm1 = new ConfirmClose(null, true);
                        konfirm1.getYa().addActionListener(new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                ErrorControl.resetErr();
                                konfirm1.setVisible(false);
                                controller.show("panel");
                                closeQuest();
                                releaseLock();
                            }
                        });
                        konfirm1.setVisible(true);
                    }
                });
                konfirm.setVisible(true);
                yakin = 0;
            }
        });

        listing.getCloseBtn().addActionListener(new ActionListener() {

            int yakin = 0;

            @Override
            public void actionPerformed(ActionEvent ae) {

                ConfirmClose konfirm = new ConfirmClose(null, true);
                konfirm.getYa().addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        yakin++;
                        konfirm.setVisible(false);
                        ConfirmClose konfirm1 = new ConfirmClose(null, true);
                        konfirm1.getYa().addActionListener(new ActionListener() {

                            @Override
                            public void actionPerformed(ActionEvent e) {
                                yakin++;
                                konfirm1.setVisible(false);
                                if (yakin == 2) {
                                    controller.show("panel");
                                    closeList();
                                }
                            }
                        });
                        konfirm1.setVisible(true);
                    }
                });
                konfirm.setVisible(true);
                yakin = 0;
            }
        });

    }

    private void closeQuest() {
        sp.util.FormControl.clearAll();
        sp.model.TableErrorModel tem = new sp.model.TableErrorModel();
        quest.getErrorTable1().getTabelError().setModel(tem);
        quest.getErrDesc().setText("");
        quest.getErrField().setText("");
        quest.getClc().first();
    }

    private void closeList() {
        sp.util.ListControl.clearAll();
        sp.model.TableErrorModel tem = new sp.model.TableErrorModel();
        quest.getErrorTable1().getTabelError().setModel(tem);
        quest.getErrDesc().setText("");
        quest.getErrField().setText("");
        listing.getClc().first();
    }

    private void releaseLock() {
        if ("0".equals(nuart)) {
        } else {
            sp.model.Dsartb4 data4 = sp.dao.DsartB4Dao.getInstance().getByID(nuart);
            data4.setIsLocked("0");
            sp.dao.DsartB1Dao.getInstance().update(data4);
        }
    }
}
